package com.android.common.base.rv;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import androidx.constraintlayout.utils.widget.ImageFilterView;
import com.android.common.R;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.luck.picture.lib.entity.LocalMedia;

public class BaseMultiMediaGridHolder extends BaseRvHolder<LocalMedia>
        implements View.OnClickListener {

    private Context mContext;

    private ImageFilterView mIfvPic;

    private ImageView mIvDel;

    public BaseMultiMediaGridHolder(View itemView) {
        super(itemView);
        mContext = itemView.getContext();
        mIfvPic = itemView.findViewById(R.id.ifv_media);
        mIvDel = itemView.findViewById(R.id.iv_del);
        mIvDel.setVisibility(View.INVISIBLE);
        initEvent();
    }

    private void initEvent() {
        mIvDel.setOnClickListener(this);
        mIfvPic.setOnClickListener(this);
    }

    @Override
    public void bindData(LocalMedia media, int position) {
        if (media == null) {
            mIvDel.setVisibility(View.GONE);
            Glide.with(mContext)
                    .load(R.mipmap.add_pic)
                    .centerCrop()
                    .into(mIfvPic);
            return;
        }
        mIvDel.setVisibility(View.VISIBLE);
        Glide.with(mContext)
                .load(media.getPath())
                .centerCrop()
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(mIfvPic);
    }

    @Override
    public void onClick(View view) {
        if (mChildViewClickListener != null) {
            mChildViewClickListener.onChildViewClick(view, getLayoutPosition());
        }
    }
}
